#!/bin/bash
#
# NOTE:
# This file will be installed in /config/faileover/active and it will 
# be called by /usr/lib/failover/f5active
# 
# - This file is for customer additions for task 
#   to be performed when failover goes to active
#
# - It is possible for this script to be called multiple times, so it
#   should not persist on the system so it won't fill up the process table

start_time=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$start_time" +%s)
echo "Start time: "$start_time > aliyun_fc.log

result=''

while true; do
curl '<内网访问地址>/remove' \
 -H 'eni-id: <peer-eni-id>' \
 -H 'eni-ip: <peer-eni-ip>'

curl '<内网访问地址>/add' \
 -H 'eni-id: <self-eni-id>' \
 -H 'eni-ip: <self-eni-ip>' > aliyun_result.log

result=`grep -c "Done" aliyun_result.log`
#echo $result

if [[ $result -eq 1 ]]; then
end_time=`date +'%Y-%m-%d %H:%M:%S'`
echo "End time: "$end_time >> aliyun_fc.log
break
fi
end_seconds=$(date --date="$end_time" +%s)
if [[ $((end_seconds - start_seconds)) -lt 15 ]]; then
echo "Failover Failed!!!!"
break
fi
sleep 1s
done

echo $(date) >> /tmp/on_active.log